﻿using System;
using System.Collections;
using System.Collections.Generic;
using DATA.BusinessObjects;
using DATA.ManagerObjects;
using NHibernate;
using DATA.Base;
using NHibernate.Criterion;

namespace BUS
{
    public class PhieuThuePhongBUS
    {
        #region Binh - Lấy phiếu thuê phòng theo mã phòng đang được thuê
        public static Phieuthuephong LayPhieuThuePhongTheoMaPhongDangThue(String mp)
        {
            Phieuthuephong ptp = null;

            IManagerFactory MF = new ManagerFactory();
            IPhieuthuephongManager PTPM = MF.GetPhieuthuephongManager();

            String sql = "from Phieuthuephong "
                         + "where Phong.Id = '" + mp + "' "
                         + "and Phong.TinhTrangPhong = 1";
            IList<Phieuthuephong> list = PTPM.GetByQuery(sql);
            
            if(list.Count > 0)
                ptp = list[0];
            return ptp;
        }
        #endregion

        #region Len - Lập phiếu thuê phòng(thêm phiếu thuê phòng)
        public static bool LapPhieuThuePhong(Phieuthuephong phieuThuePhong)
        {
            bool flag = false;
            ISession isession = NHibernateSessionManager.Instance.Session.GetISession();
            ITransaction iTrans = null;
            try
            {
                iTrans = isession.BeginTransaction();
                isession.Save(phieuThuePhong);
                iTrans.Commit();
                flag = true;
            }
            catch (Exception ex)
            {
                iTrans.Rollback();
                isession.Close();
                throw new Exception("Lỗi, không thêm phiếu thuê phòng được", ex);
            }
            return flag;
        }
        #endregion

        #region Binh - Lấy phiếu thuê phòng mới nhất
        public static Phieuthuephong LayPhieuThuePhongCuoiCung()
        {
            Phieuthuephong ptp = null;
            IManagerFactory iMF = new ManagerFactory();
            IPhieuthuephongManager ptpM = iMF.GetPhieuthuephongManager();
            String sql = "FROM Phieuthuephong ptp "
                        + "WHERE length(ptp.Id) = (Select MAX(length(ptp.Id)) "
                                                + "from Phieuthuephong ptp) "
                        + "ORDER BY ptp.Id desc";

            IList<Phieuthuephong> list = ptpM.GetByQuery(sql);
            ptp = list[0];
            return ptp;
        }
        #endregion
    }
}
